import empty from "empty-folder"
import { remote, dialog } from "electron"
import path from "path"
export default {
    clearDir(path) {
        return new Promise((resolve, reject) => {
            empty(path, false, o => {
                if (!o.error)
                    resolve(null)
                else
                    reject(o.error)
            })
        })
    },
    clearConfig() {
        localStorage.clear()
    },
    saveConfig(config) {
        localStorage.setItem("config", JSON.stringify(config));
    },
    getConfig() {
        let config = {}
        let configStr = localStorage.getItem("config")
        if (configStr == null) {
            let docPath = remote.app.getPath('documents')
            let downloadPath = path.join(docPath, "HiGame/Downloads")
            let installPath = path.join(docPath, "HiGame/Games")
            config = {
                closeApp: "0",
                installAfter: "2",
                downloadPath: downloadPath,
                installPath: installPath
            }
        } else {
            config = JSON.parse(configStr)
        }
        return config
    },
    saveGames(games) {
        localStorage.setItem("games", JSON.stringify(games));
    },
    getGames() {
        let config = []
        let configStr = localStorage.getItem("games")
        if (configStr == null) {
            config = [
                {
                    name: "侠盗猎车手5",
                    main_image: "https://img9.doubanio.com/lpic/s29296395.jpg",
                    path: "steam://rungameid/271590"
                },
                {
                    name: "魔兽争霸3：冰封王座",
                    main_image: "https://img9.doubanio.com/lpic/s28396296.jpg",
                    path: "G:/WarcraftIII/Frozen Throne.exe"
                },
                {
                    name: "泰坦之旅",
                    main_image: "https://img9.doubanio.com/lpic/s28266375.jpg",
                    path: "steam://rungameid/475150"
                },
                {
                    name: "暗黑破坏神3",
                    main_image: "https://img3.doubanio.com/lpic/s9090300.jpg",
                    path: "G:/暗黑破坏神IIIDiablo III/Launcher.exe"
                },
                {
                    name: "古墓丽影：崛起",
                    main_image: "https://img3.doubanio.com/lpic/s29757832.jpg",
                    path: "steam://rungameid/203160"
                },
                {
                    name: "古墓丽影",
                    main_image: "https://img3.doubanio.com/lpic/s25805601.jpg",
                    path: "steam://rungameid/391220"
                },
                {
                    name: "巫师 3：狂猎",
                    main_image: "https://img3.doubanio.com/lpic/s28081091.jpg",
                    path: "steam://rungameid/292030"
                },
                {
                    name: "盟军敢死队合集",
                    main_image: "https://img1.doubanio.com/lpic/s10204589.jpg",
                    path: "G:/新建文件夹/Comm2x64/CommXPC.exe"
                },

            ]
        } else {
            config = JSON.parse(configStr)
        }
        return config
    },
    selectDir(callback) {
        remote.dialog.showOpenDialog({
            properties: ['openDirectory']
        }, (files) => {
            if (files) {
                callback(files[0])
            }
        })
    },
    selectFile(callback) {
        remote.dialog.showOpenDialog({
            properties: ['openFile']
        }, (files) => {
            if (files) {
                callback(files[0])
            }
        })
    },
    selectGameFile(callback) {
        remote.dialog.showOpenDialog({
            title: "请选择游戏主程序",
            properties: ['openFile'],
            filters: [{
                name: "游戏主程序",
                extensions: ['exe']
            }]
        }, (files) => {
            if (files) {
                callback(files[0])
            }
        })
    },
    copy(text) {
        remote.clipboard.writeText(text)
    }

}